<?php
/**
 * 从小到大选择排序
 * @param array $arr
 * @return array
 */
function selectionSort(array $arr):array
{
    for ($i=0;$i < count($arr) - 1;$i++){
        $minIndex = $i;
        $min = $arr[$i];
        for ($j = $i;$j < count($arr);$j++){
            if($arr[$j] < $min){
                $minIndex = $j;
                $min = $arr[$j];
            }
        }
        if($minIndex != $i){
            $tpm = $arr[$minIndex];
            $arr[$minIndex] = $arr[$i];
            $arr[$i] = $tpm;
        }
    }

    return $arr;
}

$arr = [1,16,5,8,4,6,2,9,6,4,10,11,5,7,3,6,12];

print_r(selectionSort($arr));
/**
 * [1,2,3,4,4,5,5,6,6,6,7,8,9,10,11,12,16]
 */
